<head>
    <title>添加项目信息</title>
    <link href="/lego/static/css/bootstrap.css" rel="stylesheet"/>
    <link href="/lego/static/css/header.css" rel="stylesheet"/>
    <link href="/lego/static/css/xcConfirm.css" rel="stylesheet"/>
    <script src="/lego/static/js/jquery.1.11.3.min.js"/></script>
    <script src = "/lego/static/js/bootstrap.js" ></script>
    <script src="/lego/static/js/underscore-min.js"></script>
    <script src="/lego/static/js/common.js"></script>
    <script src="/lego/static/js/xcConfirm.js"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                url: '/lego/getNextProjectId',
                type: 'GET',
                success: function (data) {
                    if (data && data.code == 200) {
                        $('#projectId').val(data.data);
                    }
                }
            });

            $.ajax({
                url: '/lego/getJoinedTeamList',
                type: 'GET',
                success: function (data) {
                    if (data && data.code == 200) {
                        $('#teamDiv').html(_.template($('#selectTeam_template').html())({
                            data: data.data
                        }));
                    }
                }
            });

            $("#addProjectBtn").click(function(){
                //参数校验
                var formJson = $('#projectForm').toJSON();
                var teamId = formJson['teamId'];
                var projectName = formJson['projectName'];
                var env = formJson['env'];
                var ip = formJson['ip'];
                var port = formJson['port'];
                if(teamId == ""){
                    window.wxc.xcConfirm("请选择项目所属团队!!!", window.wxc.xcConfirm.typeEnum.error);
                    return;
                }
                if(projectName == ""){
                    window.wxc.xcConfirm("项目名称不能为空!!!", window.wxc.xcConfirm.typeEnum.error);
                    return;
                }
                if(env == ""){
                    window.wxc.xcConfirm("请选择环境!!!", window.wxc.xcConfirm.typeEnum.error);
                    return;
                }
                if(ip == ""){
                    window.wxc.xcConfirm("环境对应的ip不能为空!!!", window.wxc.xcConfirm.typeEnum.error);
                    return;
                }
                if(port == ""){
                    window.wxc.xcConfirm("环境对应的端口不能为空!!!", window.wxc.xcConfirm.typeEnum.error);
                    return;
                }

                //验证ip地址格式是否正确
                var pattern = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
                if(!pattern.test(ip)){
                    window.wxc.xcConfirm("ip地址格式错误!!!", window.wxc.xcConfirm.typeEnum.error);
                    return;
                }

                //验证端口格式是否正确
                pattern = /^\d{0,5}$/;
                if(!pattern.test(port)){
                    window.wxc.xcConfirm("端口格式错误!!!", window.wxc.xcConfirm.typeEnum.error);
                    return;
                }

                $.ajax({
                    url: '/lego/addProject',
                    type: 'POST',
                    data:$("#projectForm").toJSONString(),
                    contentType:'application/json;charset=UTF-8',
                    success: function (data) {
                        if (data && data.code == 200) {
                            var op = {
                                title: "提示信息",
                                btn: window.wxc.xcConfirm.btnEnum.ok,
                            }
                            if(data.msg == "添加成功"){
                                window.wxc.xcConfirm(data.msg,window.wxc.xcConfirm.typeEnum.success, op);
                            }else {
                                window.wxc.xcConfirm(data.msg,window.wxc.xcConfirm.typeEnum.error, op);
                            }

                        }
                    }
                });
            });
        });
    </script>
</head>
<body>
    <#include "../navigationBar.ftl">
    <header class="jumbotron subhead" id="overview">
        <div class="container">
            <h1>项目信息管理</h1>
        </div>
    </header>

    <form method="post" class="form-horizontal" id="projectForm">
        <div class="row">
            <label for="projectId" class="col-sm-4 control-label">项目编号</label>
            <div class="col-sm-4">
                <input type="text" class="form-control " readonly="readonly" name="projectId" id="projectId">
            </div>
        </div>
        <div class="row">
            <label for="teamId" class="col-sm-4 control-label">选择团队</label>
            <div class="col-sm-4" id="teamDiv"></div>
            <#include "../common/template.ftl">
        </div>
        <div class="row">
            <label for="projectName" class="col-sm-4 control-label">项目名称</label>
            <div class="col-sm-4">
                <input type="text" class="form-control" name="projectName" id="projectName">
            </div>
        </div>
        <div class="row">
            <label for="env" class="col-sm-4 control-label">选择环境</label>
            <div class="col-sm-4">
                <select name="env" class="form-control" id="env">
                    <option value="LIANTIAO" selected="">联调环境</option>
                    <option value="CI">ci环境</option>
                    <option value="YANLIAN">演练环境</option>
                    <option value="ONLINE">线上环境</option>
                </select>
            </div>
        </div>
        <div class="row">
            <label for="ip" class="col-sm-4 control-label">IP</label>
            <div class="col-sm-4">
                <input type="text" class="form-control" name="ip" id="ip">
            </div>
        </div>
        <div class="row">
            <label for="port" class="col-sm-4 control-label">端口</label>
            <div class="col-sm-4">
                <input type="text" class="form-control" name="port" ip="port">
            </div>
        </div>

        <div class="text-center" style="margin-top: 15px">
            <button class="btn btn-success" type="button" id="addProjectBtn">
                <span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存
            </button>
            <a class="btn btn-primary" href="/lego/projectListIndex">
                <span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>返回列表
            </a>
        </div>
    </form>
</body>
